﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using Caliburn.Micro;
using PicturePortfolio.Silverlight.ViewModels;
namespace PicturePortfolio.Silverlight.Extension
{
    public class PopupWindowManager:IWindowManager
    {
        Popup _popup = null;
        PopupContentViewModel _popupContainer = null;
        public void ShowDialog(object rootModel, object context = null)
        {
            if (_popup == null)
                _popup = new Popup();

            _popup.Child = this.CreatePopupContainer(rootModel);
            _popup.HorizontalOffset = 150;
            _popup.IsOpen = true;
        }

        private UIElement CreatePopupContainer(object rootModel)
        {
            if (_popupContainer == null)
                this._popupContainer = new PopupContentViewModel(rootModel);

            var popupContainerView = ViewLocator.LocateForModel(_popupContainer, null, null);
            ViewModelBinder.Bind(_popupContainer, popupContainerView, null);
            IChildWindow windowChild = _popupContainer as IChildWindow;
            if (windowChild != null)
            {
                windowChild.PopupCloseDelegate = new CloseDelegate(this.ClosePopup);
            }

            return popupContainerView;
        }

        public void ClosePopup()
        {
            _popupContainer = null;
            _popup.IsOpen = false;
        }
    }
}
